package com.wiretap.dao.impl;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

import com.wiretap.dao.MotionDao;
import com.wiretap.po.Motion;
import com.wiretap.util.Pager;

@SuppressWarnings("unchecked")
public class MotionDaoImpl implements MotionDao {

	private SessionFactory sessionFactory;

	@Override
	public void addMotion(Motion motion) throws Exception {
		try {
			this.sessionFactory.getCurrentSession().persist(motion);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("addMotion error");
		}
	}

	@Override
	public int removeMotion(String id) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from Motion m where m.id=:id").setString("id", id).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("removeMotion error");
		}
	}

	@Override
	public Pager<Motion> getAllMotionByPhone(String phone, int currentPage, int pageSize) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			int totalCount = sesion.createQuery("from Motion a where a.phone =:phone order by a.addDate desc").setString("phone", phone).list().size();
			int pageCount = (totalCount % pageSize == 0) ? totalCount / pageSize : (totalCount / pageSize) + 1;

			List<Motion> list = sesion.createQuery("from Motion a where a.phone =:phone order by a.addDate desc").setString("phone", phone).setFirstResult((currentPage - 1) * pageSize)
					.setMaxResults(pageSize).list();
			Pager<Motion> pager = new Pager<Motion>(list, currentPage, totalCount, pageCount);
			return pager;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getAllMotionByPhone error");
		}
	}

	@Override
	public List<Motion> getUnExcuteMotionByPhone(String phone) throws Exception {
		try {
			Session sesion = this.sessionFactory.getCurrentSession();
			List<Motion> list = sesion.createQuery("from Motion a where a.phone = :phone and a.executed = :executed").setString("phone", phone).setBoolean("executed", false).list();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getUnExcuteMotionByPhone error");
		}
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	@Override
	public Motion getMotionById(String id) throws Exception {
		try {
			return (Motion) sessionFactory.getCurrentSession().get(Motion.class, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("getMotionById error");
		}
	}

	@Override
	public void updateMotion(Motion motion) throws Exception {
		try {
			sessionFactory.getCurrentSession().update(motion);
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("updateMotion error");
		}
	}

	@Override
	public int deleteMotionByPhone(String phone) throws Exception {
		try {
			return sessionFactory.getCurrentSession().createQuery("delete from Motion m where m.phone=:phone").setString("phone", phone).executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new Exception("deleteMotionByPhone error");
		}
	}

}
